//
//  ABUDCustomSplashAdapter.m
//  ABUDemo
//
//  Created by bytedance on 2021/10/21.
//  Copyright © 2021 bytedance. All rights reserved.
//

#import "JiGuangAdsSplashAdapter.h"
#import <JgAdsSDK/JgAdsSDK.h>
#import "JiGuangAdsSplashScapegoat.h"
@interface JiGuangAdsSplashAdapter ()<JgAdsSplashAdDelegate>{
    CFAbsoluteTime _startTime;
}
@property (nonatomic, strong) JiGuangAdsSplashScapegoat *scapegoat;

@property (nonatomic, strong) JgAdsSplashAd *splashAd;

@property (nonatomic, strong) UIView *customBottomView;

@property (nonatomic, strong)UIWindow *showWindow;

@property (nonatomic, assign) BOOL isBidType;
@end

@implementation JiGuangAdsSplashAdapter
- (JiGuangAdsSplashScapegoat *)scapegoat{
    if (_scapegoat == nil) {
        _scapegoat = [[JiGuangAdsSplashScapegoat alloc] init];
        _scapegoat.splashAdapter = self;
    }
    return _scapegoat;
}

- (BUMMediatedAdStatus)mediatedAdStatus {
    return BUMMediatedAdStatusNormal;
}

- (void)loadSplashAdWithSlotID:(nonnull NSString *)slotID andParameter:(nonnull NSDictionary *)parameter {
    self.customBottomView = parameter[BUMAdLoadingParamSPCustomBottomView];
    self.splashAd = [JgAdsSplashAd new];
    self.splashAd.delegate = self.scapegoat;
    self.splashAd.posId = slotID;
    self.splashAd.hiddenSkipView = NO;
    self.splashAd.viewController = self.bridge.viewControllerForPresentingModalView;
    [self.splashAd loadAdWithBottomView:self.customBottomView];
    _startTime =CFAbsoluteTimeGetCurrent();
    if([parameter[BUMAdLoadingParamBiddingType] integerValue] == 1){
        self.isBidType = YES;
    }else{
        self.isBidType = NO;
    }
    self.scapegoat.isBidType = self.isBidType;
    if (self.isBidType) {
        NSLog(@"极光ads==========极光ads竞价广告");
    }
}

- (void)showSplashAdInWindow:(nonnull UIWindow *)window parameter:(nonnull NSDictionary *)parameter {
    self.showWindow = window;
    if (self.showWindow == nil) {
        self.showWindow = [UIApplication sharedApplication].keyWindow;
    }
    if (self.isBidType){
        [self.splashAd sendWinNotificationWithPrice:self.splashAd.bidPrice];
    };
    [self.splashAd showInWindow:window];
}

-(void)didReceiveBidResult:(BUMMediaBidResult *)result{
    
    if (!self.splashAd) return;
    if (result.win) {
        NSLog(@"开屏==========竞价成功:%ld",(long)result.winnerPrice);
//        [self.splashAd sendWinNotificationWithPrice:result.winnerPrice];
    } else {
        NSLog(@"开屏==========竞价失败:%@,竞赢价格：%ld",result.lossDescription,(long)result.winnerPrice);
        JgAdsBiddingLossReason reason ;
        if ([result.lossDescription isEqualToString:@"timeout"]) {
            reason = JgAdsBiddingLossReasonOther;
        }else if ([result.lossDescription isEqualToString:@"low price"]){
            reason = JgAdsBiddingLossReasonLowPrice;
        }else {
            reason = JgAdsBiddingLossReasonOther;
        }
        [self.splashAd sendWinFailNotificationReason:reason winnerPirce:result.winnerPrice];
       
    }
    
}
-(void)dealloc{
    [self dismissSplashView];
}

/**
 停止监听所有回调，一般在 close 时触发，第一次触发 close 后，就停止监听
 */
-(void)dismissSplashView{
    _splashAd = nil;
}
@end
